import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Chat from '../views/Chat.vue'

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/chat',
        name: 'Chat',
        component: Chat,
        beforeEnter: (to, from, next) => {
            const user = JSON.parse(localStorage.getItem('currentUser') || 'null')
            if (!user) {
                next({ name: 'Login' })
            } else {
                next()
            }
        }
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

export default router
